namespace ASPNET
{
    using ASPNET.StarterKit.IssueTracker.BusinessLogicLayer;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Globalization;
    using System.Threading;
    using System.Web;
    using System.Web.Security;

    public class Global : HttpApplication
    {
        private IContainer components;

        public Global()
        {
            base.BeginRequest += new EventHandler(this.Application_BeginRequest);
            base.AuthenticateRequest += new EventHandler(this.Application_AuthenticateRequest);
            this.InitializeComponent();
        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {
            string userInformation = string.Empty;
            if (this.Request.IsAuthenticated)
            {
                if ((this.Request.Cookies["userroles"] == null) || (StringType.StrCmp(this.Request.Cookies["userroles"].Value, "", false) == 0))
                {
                    ITUser user = ITUser.GetUserByUsername(this.Context.User.Identity.Name);
                    if (user == null)
                    {
                        ITUser newUser = new ITUser(DefaultValues.GetUserIdMinValue(), this.Context.User.Identity.Name, Globals.DefaultRoleForNewUser);
                        newUser.Save();
                        user = newUser;
                    }
                    userInformation = StringType.FromInteger(user.Id) + ";" + user.RoleName + ";" + user.Username;
                    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, this.Context.User.Identity.Name, DateTime.Now, DateTime.Now.AddHours(1.0), false, userInformation);
                    string cookieStr = FormsAuthentication.Encrypt(ticket);
                    this.Response.Cookies["userroles"].Value = cookieStr;
                    this.Response.Cookies["userroles"].Path = "/";
                    this.Response.Cookies["userroles"].Expires = DateTime.Now.AddMinutes(1.0);
                    this.Context.User = new CustomPrincipal(this.Context.User.Identity, user.Id, user.RoleName, user.Username);
                }
                else
                {
                    string[] info = FormsAuthentication.Decrypt(this.Context.Request.Cookies["userroles"].Value).UserData.Split(new char[] { ';' });
                    this.Context.User = new CustomPrincipal(this.User.Identity, Convert.ToInt32(info[0].ToString()), info[1].ToString(), info[2].ToString());
                }
            }
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            if (this.Request.UserLanguages != null)
            {
                try
                {
                    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(this.Request.UserLanguages[0]);
                }
                catch (Exception exception1)
                {
                    ProjectData.SetProjectError(exception1);
                    ProjectData.ClearProjectError();
                }
            }
            else
            {
                Thread.CurrentThread.CurrentCulture = new CultureInfo("en-us");
            }
            Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
        }

        [DebuggerStepThrough]
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
        }
    }
}

